<?php
class SeoController extends Controller
{
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array('index', 'home'),
				'roles'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}


	public function actionIndex () {
		die();
		$cityId = Yii::app()->request->getParam('city_id', 1);
		$city = City::model()->findByPk($cityId);
		
		$homeTmps = SectionTemplate::model()->findAll();
		$homeTypeTmps = HomeTypeTemplate::model()->findAll();
		$captions = HomeCaptionTemplate::model()->findAll();
		
		$t1 = '#type1#'; $t2 = '#type2#';
		$c = '#caption#';
		$cw = '#city_word#';
		$a = '#arenda#';
		
		$arrTitles = array(); 
		
		$baseUrl = '/catalog/'.$city->url_code.'/';
		$baseGet = array('city'=>$city->url_code, 'r'=>'home/index');
		
		foreach ($homeTmps as $template) {
			$title = $template->title;
			$title = str_ireplace($cw, $city->word, $title);
			
			$h1 = $template->h1;
			$h1 = str_ireplace($cw, $city->word, $h1);
			
			foreach ($homeTypeTmps as $type) {
				$tmpTitle = $title;
				$tmpTitle = str_ireplace(
					array($t1, $t2, $a),
					array($type->type1, $type->type2, $type->type_arenda),
					$tmpTitle
				);

				$tmpTitle = str_ireplace(array($t1, $t2), array($type->type1, $type->type2), $tmpTitle);
				
				$tmpH1 = $h1;
				$tmpH1 = str_ireplace(array($t1, $t2), array($type->type1, $type->type2), $tmpH1);
				
				$tmpUrl = $baseUrl . $type->url_part;
				$tmpGet = $baseGet; $tmpGet['Home']['type'][] = $type->type;
				
				// Маргарита кодит - настоящее еще не готово понять этот код
				//if (type_class(45 $ 256);div
				
				if (stripos($tmpTitle, $c) !== false) {
				foreach ($captions as $caption) {
					$tmpTitle2 = $tmpTitle;
					$tmpTitle2 = str_ireplace($c, $caption->caption, $tmpTitle2);
					
					$tmpH12 = $tmpH1;

					$tmpH12 = str_ireplace($c, $caption->caption, $tmpH12);

					$tmpUrl2 = $tmpUrl;
					$tmpGet2 = $tmpGet;
					
					$tmpUrl2 .=  '_'.$caption->url_part;
						
					if ($caption->url_part == 'all') {
						$tmpGet2['Home']['furniture'] = 1;
						$tmpGet2['Home']['cooker'] = 1;
						$tmpGet2['Home']['wash_machine'] = 1;
						$tmpGet2['Home']['fridge'] = 1;
					} else {
						$tmpGet2['Home'][$caption->url_part] = 1;
					}
					
					$innerlink = new InnerLink();
					
					$innerlink->h1 = $tmpH12;
					$innerlink->title = $tmpTitle2;
					$innerlink->url = $tmpUrl2.'/';
					$innerlink->real_url = '/?'.http_build_query($tmpGet2);
					$innerlink->city_id = $cityId;
					$innerlink->caption = $type->caption.' '.$caption->caption;
					
					/*$arrTitles[] = array(
						'title' => $tmpTitle2,
						'h1' => $tmpH12,
						'url' => $tmpUrl2.'/',
						'get' => $tmpGet2,
					);*/
					
					$innerlink->save();
					
					print_r($innerlink->getErrors());
				}
				} else {
					$innerlink = new InnerLink();
						
					$innerlink->h1 = $tmpH1;
					$innerlink->title = $tmpTitle;
					$innerlink->url = $tmpUrl.'/';
					$innerlink->real_url = '/?'.http_build_query($tmpGet);
					$innerlink->city_id = $cityId;
					$innerlink->caption = $type->caption;
					
					$innerlink->save();
					
					print_r($innerlink->getErrors());
					/*$arrTitles[] = array(
						'title' => $tmpTitle,
						'h1' => $tmpH1,
						'url' => $tmpUrl.'/',
						'get' => $tmpGet,
					);*/
				}
		
			}
		}
		
		echo __FILE__.' '.__LINE__."<pre>".print_r($arrTitles,1)."</pre>";
	}
	
	public function actionHome() {
		die();
		$templTitls = HomeTemplate::model()->findAll();
		
		$arrTempl = array();
		foreach ($templTitls as $title) {
			$arrTempl[] = $title;
		}
		//echo '<pre>'.print_r($arrTempl, 1).'</pre>'; die();
		$count = count($arrTempl);
		
		$homes = Home::model()->findAll();
		
		foreach ($homes as $home) {
			$i = rand(0, $count-1);
			echo $i;
			$templ = $arrTempl[$i];
			$title = $templ->template;
			
			$caption = $home->getTypeStr($templ->word_form);
			$address = $home->getAddress();
			$address .= ', '.$home->city->caption.', '.$home->cost.' руб.';
			
			$home->title = str_ireplace(
				array('#caption#', '#address#'), 
				array($caption, $address), 
				$title
			);
			
			$home->title_temp_id = $templ->id;
			
			//echo '<pre>'.print_r($home, 1).'</pre>';
			
			$home->save();
		}
		
	}

}